package cn.xorange.love.controller;

import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.bean.SystemCode;
import cn.xorange.commons.core.basic.controller.BaseController;
import cn.xorange.commons.core.exception.SysException;
import cn.xorange.commons.file.service.QiNiuUploadService;
import cn.xorange.commons.security.annotation.PassToken;
import cn.xorange.commons.security.service.SecurityService;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.utils.configuration.constants.GlobalConstants;
import cn.xorange.love.entity.AppUserPhoto;
import cn.xorange.love.entity.AppUserProfile;
import cn.xorange.love.service.AppUserPhotoService;
import cn.xorange.love.service.AppUserProfileService;
import com.alibaba.fastjson2.JSON;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author : yangjian
 * date : 2024/3/8
 * description :
 */
@RestController
@RequestMapping("/app/user")
public class AppUserProfileController extends BaseController {

    @Resource
    SecurityService securityService;
    @Resource
    QiNiuUploadService qiNiuUploadService;

    @Resource
    AppUserProfileService profileService;
    @Resource
    AppUserPhotoService photoService;

    @PostMapping("/profile")
    public ResultBean updateUserProfile(@RequestBody AppUserProfile userProfile){

        Long userId = userProfile.getUserId();
        boolean upMe = false;
        if(Objects.isNull(userId)){
            userId = SecurityUtils.getUserId();
            upMe = true;
        }
        userProfile.where.equal("userId", userId);
        profileService.update(userProfile);
        if(upMe){
            securityService.refreshUserInfoCache(JSON.toJSONString(userProfile));
        }
        return ResultBean.onSuccess();
    }

    @GetMapping(value = {"/profile","/profile/{id}"})
    public ResultBean getUserProfile(@PathVariable(value = "id", required = false)Long id){
        if(Objects.isNull(id)){
            id = SecurityUtils.getUserId();
        }
        AppUserProfile profile = new AppUserProfile();
        profile.setUserId(id);
        profile = this.profileService.getEntity(profile);
        return ResultBean.onSuccess(profile);
    }

    @GetMapping("/list")
    public PageBean getUserList(AppUserProfile user){
        if(Objects.isNull(user.getLifeCity()) || Objects.isNull(user.getSex())){
            throw SysException.paramException(SystemCode.USER_PARAM_NULL.getMsg());
        }
        user.where.equal("lifeCity",user.getLifeCity());
        user.where.equal("sex", user.getSex());
        if(Objects.nonNull(user.getWProvince())){
            user.where.equal("province",user.getWProvince());
        }
        if(Objects.nonNull(user.getWCity())){
            user.where.equal("city",user.getWCity());
        }
        if(Objects.nonNull(user.getWAgeMin())){
            user.where.gte("birthday", user.getWAgeMin());
        }
        if(Objects.nonNull(user.getWAgeMax())){
            user.where.lte("birthday", user.getWAgeMax());
        }
        if(Objects.nonNull(user.getWHeightMin())){
            user.where.gte("height", user.getWHeightMin());
        }
        if(Objects.nonNull(user.getWHeightMax())){
            user.where.lte("height", user.getWHeightMax());
        }
        if(Objects.nonNull(user.getWEducation())){
            Object[] edu = user.getWEducation().split(",");
            user.where.in("education", edu );
        }
        if(Objects.nonNull(user.getWCompany())){
            Object[] com = user.getWCompany().split(",");
            user.where.in("companyType", com);
        }
        if(Objects.nonNull(user.getWIncome())){
            Object[] income = user.getWIncome().split(",");
            user.where.in("income", income);
        }
        if(Objects.nonNull(user.getWMarital())){
            Object[] type = user.getWMarital().split(",");
            user.where.in("maritalType", type );
        }

        super.startPage();
        List<AppUserProfile> lst = this.profileService.getList(user);

        List<AppUserProfile> lst2 = new ArrayList<>();
        for(int i=0; i<10; i++){
            lst2.addAll(lst);
        }

        return new PageBean(lst2);
    }



    @PassToken
    @GetMapping("/photo")
    public ResultBean getPhotos(AppUserPhoto photo){
        Long userId = SecurityUtils.getUserId();
        photo.setUserId(userId);
        List<AppUserPhoto> lst = photoService.getList(photo);
        return ResultBean.onSuccess(lst);
    }

    @PassToken
    @GetMapping("/photo/count")
    public ResultBean getPhotosCount(){
        Long userId = SecurityUtils.getUserId();
        AppUserPhoto photo = new AppUserPhoto();
        photo.setUserId(userId);
        int num = photoService.count(photo);
        return ResultBean.onSuccess(num);
    }

    @PostMapping("/photo")
    public ResultBean addPhoto(@RequestBody AppUserPhoto photo){
        Long userId = SecurityUtils.getUserId();
        photo.setUserId(userId);
        photo.setStatus(GlobalConstants.FLAG_EXAMINE_NULL);
        this.photoService.add(photo);
        return ResultBean.onSuccess();
    }

    @PutMapping("/photo")
    public ResultBean updatePhotoInfo(@RequestBody AppUserPhoto photo){
        if(Objects.isNull(photo.getId())){
            return ResultBean.onFail("id为空");
        }else {
            this.photoService.update(photo);
            return ResultBean.onSuccess();
        }
    }

    @DeleteMapping("/photo/{id}")
    public ResultBean delPhoto(@PathVariable Long id){
        AppUserPhoto photo = this.photoService.getById(id);
        if(Objects.nonNull(photo)){
            qiNiuUploadService.delFile(photo.getPreview());
            qiNiuUploadService.delFile(photo.getPath());
            photoService.delete(photo);
        }
        return ResultBean.onSuccess();
    }



}
